package org.watchrecords.pages.crime;

import java.util.Arrays;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.watchrecords.annotations.InstantiationPermission;
import org.watchrecords.constants.Gender;
import org.watchrecords.constants.OffenderStatus;
import org.watchrecords.constants.Permission;
import org.watchrecords.constants.Race;
import org.watchrecords.dbo.Offender;
import org.watchrecords.elements.BasicForm;
import org.watchrecords.managers.OffenderManager;
import org.watchrecords.pages.BasicPage;
import org.watchrecords.util.EnumHelper;

import com.google.inject.Inject;

@InstantiationPermission(Permission.EDIT_OFFENDER)
public class EditOffenderPage extends BasicPage {
    @Inject private OffenderManager offenderManager;

    public EditOffenderPage(PageParameters pageParameters) {
        super(pageParameters);
        Integer offenderId = getPageParameters() == null ? null : getPageParameters().getAsInteger("offenderId");
        Offender offender = offenderId == null ? new Offender() : offenderManager.getOffender(offenderId);
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        
        add(new Label("header", offenderId == null ? "Add Offender": "Edit Offender: " + offender.getFullName()));
        add(new EditOffenderForm("form", offender));
        add(feedbackPanel);
    }
    
    private class EditOffenderForm extends BasicForm<Offender> {
        private static final long serialVersionUID = 2965819179542583966L;

        public EditOffenderForm(String id, Offender off) {
            super(id, off);
            
            add(new TextField<String>("firstName").setRequired(false));
            add(new TextField<String>("nickname").setRequired(false));
            add(new TextField<String>("lastName").setRequired(false));
            add(new TextField<Integer>("argentNode").setRequired(false));
            add(new TextField<Integer>("unpaidFine"));
            add(new TextArea<String>("description").setRequired(false));
            add(new TextArea<String>("possessions").setRequired(false));
            add(new DropDownChoice<OffenderStatus>("status", Arrays.asList(OffenderStatus.values())));
            add(new DropDownChoice<Race>("race", EnumHelper.getEnumList(Race.class)));
            add(new DropDownChoice<Gender>("gender", EnumHelper.getEnumList(Gender.class)));

            add(new Button("cancel") {
                private static final long serialVersionUID = -7204043210850354787L;

                @Override
                public void onSubmit() {
                    setResponsePage(ViewOffendersPage.class);
                }
            }.setDefaultFormProcessing(false));
        }
        
        @Override
        protected void doSubmit() {
            Offender offender = this.getModelObject();
            offenderManager.saveOffender(offender);
            setResponsePage(ViewOffendersPage.class);
        }
    }
}
